package com.ten.course.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ten.common.core.entity.*;
import com.ten.common.core.vo.Result;
import com.ten.course.service.CourseListService;
import com.ten.course.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author ：张成（3092211427@qq.com）
 * @date ：Created in 2022/12/7 18:02
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/core/course")
@Api(tags = "课程列表api接口")
public class CourseListController {
    @Autowired
    private CourseListService courseListService;

    @Autowired
    private OssEntity ossEntity;

    @ApiOperation(value = "根据条件进行分页")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页码", name = "pageCurrent"),
            @ApiImplicitParam(value = "展示显示的页数", name = "pageSize")
    })
    @PostMapping("findCourse/{pageCurrent}/{pageSize}")
    public Result<Page<EduCourse>> findCourse(
            @PathVariable Integer pageCurrent,
            @PathVariable Integer pageSize,
            @RequestBody CourseCheckVo courseCheckVo) {
        return courseListService.findCourse(pageCurrent, pageSize, courseCheckVo);
    }

    /**
     * 根据id修改商品是否上下架
     *
     * @param listChangeStatusVo id status
     * @return
     */
    @ApiOperation(value = "根据id修改商品是否上下架")
    @PostMapping("changeStatus")
    public Result changeStatus(@RequestBody ListChangeStatusVo listChangeStatusVo) {
        return courseListService.changeStatus(listChangeStatusVo);
    }

    /**
     * 根据课程id修改课程的信息
     *
     * @param courseChangeVo title，price，editor课程简介
     * @return
     */
    @ApiOperation(value = "根据课程id修改课程的信息")
    @PostMapping("updateCourse")
    public Result updateCourse(@RequestBody CourseChangeVo courseChangeVo) {
        return courseListService.updateCourse(courseChangeVo);
    }

    /**
     * 根据课程id修改isAdd字段
     * @param eduCourse 课程类
     * @return
     */
    @ApiOperation(value = "根据课程id修改isAdd字段")
    @PostMapping("updateIsAdd")
    public Result<Boolean> updateIsAdd(@RequestBody EduCourse eduCourse){
        return courseListService.updateIsAdd(eduCourse);
    }

    /*************************客户端controller*********************/

    /**
     * 根据老师的手机号查询该老师下的所有的课程信息
     *
     * @param pageCurrent   当前页数
     * @param pageSize      每页显示的行数
     * @param teacherMobile 老师的手机号
     * @param checkStatus   课程审核的状态
     * @return
     */
    @ApiOperation(value = "根据老师的手机号查询该老师下的所有的课程信息")
    @GetMapping("getCourseByTeacherPhone/{pageCurrent}/{pageSize}/{teacherMobile}/{checkStatus}")
    public Result<IPage<EduCourse>> getCourseByTeacherPhone(
            @PathVariable Integer pageCurrent,
            @PathVariable Integer pageSize,
            @PathVariable String teacherMobile,
            @PathVariable String checkStatus
    ) {
        return courseListService.getCourseByTeacherPhone(pageCurrent, pageSize, teacherMobile, checkStatus);
    }

    /**
     * 根据前端传过来的信息，根据课程id对课程信息，包括课程的分类和描述等进行修改
     *
     * @param updateCourseSubjectVo 更新课程信息Vo类
     * @return
     */
    @ApiOperation(value = "根据课程id对课程信息，包括课程的分类和描述等进行修改")
    @PostMapping("updateCourseSubject")
    public Result updateCourseSubject(@RequestBody UpdateCourseSubjectVo updateCourseSubjectVo) {
        return courseListService.updateCourseSubject(updateCourseSubjectVo);
    }

    @PostMapping("uploadMsg")
    @ApiOperation(value = "阿里云上传图片请求网址")
    public String uploadMsg(MultipartFile file) {
        String filename = file.getOriginalFilename();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossEntity.getEndpoint(), ossEntity.getAccessKeyId(), ossEntity.getAccessKeySecret());
        try {
            try {
                String newFileName = UUID.randomUUID() + filename;
                ossClient.putObject(ossEntity.getBucketName(), newFileName, new ByteArrayInputStream(file.getBytes()));
                return "https://" + ossEntity.getBucketName() + "." + ossEntity.getEndpoint() + "/" + newFileName;
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }

    /**
     * 根据courseId查询EduCourse并封装到list中
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据courseId查询EduCourse并封装到list中")
    @PostMapping("selectByCourseId/{courseId}")
    public Result<List<EduCourse>> selectByCourseId(@PathVariable String courseId){
        return courseListService.selectByCourseId(courseId);
    }

    /**
     * 根据fontFindCourseVo中包含的课程分类id来查询里面所含有的课程信息
     * @param pageCurrent 当前页码
     * @param pageSize 显示条数
     * @param fontFindCourseVo 包含课程分类id和isFree信息
     * @return
     */
    @ApiOperation(value = "根据fontFindCourseVo中包含的课程分类id来查询里面所含有的课程信息")
    @PostMapping("fontFindCourse/{pageCurrent}/{pageSize}")
    public Result<Page<EduCourse>> fontFindCourse(@PathVariable Integer pageCurrent,
                                 @PathVariable Integer pageSize,
                                 @RequestBody FontFindCourseVo fontFindCourseVo){
        return courseListService.fontFindCourse(pageCurrent,pageSize,fontFindCourseVo);
    }

    /**
     * 根据课程id查询课程和教师信息
     * @param id 课程id
     * @return
     */
    @ApiOperation(value = "根据课程id查询课程和教师信息")
    @PostMapping("queryAllCoursesAndTeacherName/{id}")
    public Result<CourseTeacherVo> queryAllCoursesAndTeacherName(@PathVariable String id){
        return courseListService.queryAllCoursesAndTeacherName(id);
    }

    /**
     * 根据手机号查询该视频信息是否该讲师发布的
     * @param courseId
     * @param mobile
     * @return
     */
    @ApiOperation(value = "根据手机号查询该视频信息是否该讲师发布的")
    @PostMapping("queryAllById/{courseId}/{mobile}")
    public Result queryAllById(@PathVariable String courseId,
                               @PathVariable String mobile){
        return courseListService.queryAllById(courseId,mobile);
    }

    /**
     * 根据课程id和会员id判断当前账号是否为讲师
     * @param courseId
     * @param huiYuanId
     * @return
     */
    @ApiOperation(value = "根据课程id和会员id判断当前账号是否为讲师")
    @PostMapping("queryMemberStatus/{courseId}/{huiYuanId}")
    public Result<TbMember> queryMemberStatus(@PathVariable String courseId,
                                              @PathVariable String huiYuanId){
        return courseListService.queryMemberStatus(courseId,huiYuanId);

    }

    @ApiOperation(value = "通过老师id和课程名字查询出该老师具有的课程信息")
    @PostMapping("findCoursesByTeacherId/{pageCurrent}/{pageSize}")
    public Result<Page<EduCourse>> findCoursesByTeacherId(@PathVariable Integer pageCurrent,
                                         @PathVariable Integer pageSize,
                                         @RequestBody CourseGuanZhuVo courseGuanZhuVo){
        System.out.println(courseGuanZhuVo);
        return courseListService.findCourseByTeacherId(pageCurrent,pageSize,courseGuanZhuVo);
    }

    /**
     * 查询出前台专区中应该显示的课程有哪些，供首页微服务进行调用
     * @return
     */
    @ApiOperation(value = "查询出前台专区中应该显示的课程有哪些，供首页微服务进行调用")
    @GetMapping("getCoursesByConditions")
    public Result<List<EduCourse>> getCoursesByConditions(){
        return courseListService.getCoursesByConditions();
    }

    /**
     * 将专区中可以添加的数据都展现出来，供首页服务进行调用
     * @param pageCurrent
     * @param pageSize
     * @param courseCheckVo
     * @return
     */
    @ApiOperation(value = "查询出专区中可以添加的课程内容")
    @PostMapping("getAllDetail/{pageCurrent}/{pageSize}")
    public Result<Page<EduCourse>> getAllDetail(
            @PathVariable Integer pageCurrent,
            @PathVariable Integer pageSize,
            @RequestBody CourseCheckVo courseCheckVo
    ){
        return courseListService.getAllDetail(pageCurrent,pageSize,courseCheckVo);
    }
}
